const AK = "NCm840YS7MC0DasfNz995dzx"
const SK = "5W9i4hFHTpHd9InYeBPtQmFGvxuVkJFC"

export async function main(image) {
    try {
        const accessToken = await getAccessToken();



        const result = await uni.request({
            url: `https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=${accessToken}`,
            method: 'POST',
            header: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Accept': 'application/json'
            },
            data: {
                'image': image,
                'detect_direction': 'false',
                'detect_language': 'false',
                'paragraph': 'false',
                'probability': 'false'
            }
        });
        return result;
    } catch (error) {
        console.error('OCR识别失败:', error);
        throw new Error('OCR识别失败: ' + error.message);
    }
}

/**
 * 使用 AK，SK 生成鉴权签名（Access Token）
 */
async function getAccessToken() {
    try {
        const result = await uni.request({
            url: `https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=${AK}&client_secret=${SK}`,
            method: 'POST'
        });

        return result.data.access_token;
    } catch (error) {
        console.error('获取AccessToken失败:', error);
        throw new Error('获取AccessToken失败: ' + error.message);
    }
}

/**
 * 获取文件base64编码（uni-app环境）
 */
function getFileContentAsBase64(path) {
    return new Promise((resolve, reject) => {
        uni.getFileSystemManager().readFile({
            filePath: path,
            encoding: 'base64',
            success: res => {
                resolve(res.data);
            },
            fail: err => {
                console.error('读取文件失败:', err);
                reject(new Error('读取文件失败: ' + err.errMsg));
            }
        });
    });
}